<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Swoole\Coroutine;
use Swoole\Coroutine\Channel;
use Swoole\Coroutine\Http\Client;
use Swoole\Coroutine\Server;
use function Swoole\Coroutine\run;

class ProcessTest extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'app:process';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        run(function () {
            echo 'run id:' .  Coroutine::getcid() . '~~pid:' . Coroutine::getPcid() . PHP_EOL;
            Coroutine::create(function (){
                echo ' go id :' . Coroutine::getCid() . '~~pid:' . Coroutine::getPcid() . PHP_EOL;
                $channel = new Channel(3);
                for ($i = 0; $i < 3; $i++) {
                    Coroutine::create(function () use ($i, $channel) {
                        $channel->push($i);
                        echo $i . 'loop go id :' . Coroutine::getCid() . '~~pid:' . Coroutine::getPcid() . PHP_EOL;
                        var_dump(Coroutine::exists($i)) . PHP_EOL;
                        var_dump(Coroutine::exists($i+10)) . PHP_EOL;
                    });
                    echo 'channel:::' . $channel->pop() . PHP_EOL;
                }
            });
        });
        exit();
        $serv = new Server("127.0.0.1", 9503, SWOOLE_BASE);

        $serv->on('request', function ($req, $resp) {
            $chan = new Channel(2);
            go(function () use ($chan) {
                $cli = new Swoole\Coroutine\Http\Client('www.qq.com', 80);
                $cli->set(['timeout' => 10]);
                $cli->setHeaders([
                    'Host' => "www.qq.com",
                    "User-Agent" => 'Chrome/49.0.2587.3',
                    'Accept' => 'text/html,application/xhtml+xml,application/xml',
                    'Accept-Encoding' => 'gzip',
                ]);
                $ret = $cli->get('/');
                $chan->push(['www.qq.com' => $cli->body]);
            });

            go(function () use ($chan) {
                $cli = new Client('www.163.com', 80);
                $cli->set(['timeout' => 10]);
                $cli->setHeaders([
                    'Host' => "www.163.com",
                    "User-Agent" => 'Chrome/49.0.2587.3',
                    'Accept' => 'text/html,application/xhtml+xml,application/xml',
                    'Accept-Encoding' => 'gzip',
                ]);
                $ret = $cli->get('/');
                $chan->push(['www.163.com' => $cli->body]);
            });

            $result = [];
            for ($i = 0; $i < 2; $i++)
            {
                $result += $chan->pop();
            }
            $resp->end(json_encode($result));
        });
        $serv->start();
//        exit;
//
//        for ($n = 1; $n <= 3; $n++) {
//            $process = new Process(function () use ($n) {
//                echo 'Child #' . getmypid() . " start and sleep {$n}s" . PHP_EOL;
//                sleep($n);
//                echo 'Child #' . getmypid() . ' exit' . PHP_EOL;
//            });
////            $process->pid;
//            $process->start();
//        }
////        for ($n = 3; $n--;) {
////            $status = Process::wait(true);
////            echo "Recycled #{$status['pid']}, code={$status['code']}, signal={$status['signal']}" . PHP_EOL;
////        }
//        echo 'Parent #' . getmypid() . ' exit' . PHP_EOL;
    }
}
